package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.AdminTable;
import com.neuedu.pojo.CadreTable;
import com.neuedu.pojo.PoorFamily;
import com.neuedu.pojo.Result;
import com.neuedu.service.CadreTableService;
import com.neuedu.mapper.CadreTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【cadre_table(干部信息表)】的数据库操作Service实现
* @createDate 2025-06-17 09:04:08
*/
@Service
public class CadreTableServiceImpl extends ServiceImpl<CadreTableMapper, CadreTable>
    implements CadreTableService{
    @Autowired
    private CadreTableMapper cadreTableMapper;

    @Override
    public Result<Void> addCadreTable(CadreTable cadreTable) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        if(cadreTable.getPassword()==null){
            cadreTable.setPassword("123456");
        }
        cadreTable.setIsDelete(0);
        cadreTable.setCreateTime(new Date());
        cadreTable.setUpdateTime(new Date());
        // 添加
        int insert = this.cadreTableMapper.insert(cadreTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<List<CadreTable>> getCadreTables(Integer currentPage, Integer pageSize, String name, String role) {
        // 设置条件
        Page<CadreTable> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<CadreTable> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),CadreTable::getName,name)
                .like(StringUtils.isNotEmpty(role),CadreTable::getRoleType,role);
        // 查询
        this.cadreTableMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateCadreTable(CadreTable cadreTable) {
        cadreTable.setUpdateTime(new Date());
        // 添加
        int insert = this.cadreTableMapper.updateById(cadreTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteCadreTable(Integer id) {
        LambdaUpdateWrapper<CadreTable> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CadreTable::getIsDelete,1)
                .eq(CadreTable::getCadreId,id);
        int update = this.cadreTableMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<CadreTable> loginCadre(String number, String password) {
              // 2. 根据number查询数据库
        CadreTable cadre=cadreTableMapper.selectByNumber(number);
        if (cadre == null) {
            return Result.fail("用户不存在");
        }

        // 3. 比较密码和手机号
        if (password.equals(cadre.getPassword())) {
            return Result.success(cadre,"登录成功");
        } else {
            return Result.fail("密码错误");
        }
    }

    @Override
    public Result<Void>updatePassword(String number,String password){
        LambdaUpdateWrapper<CadreTable>wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(CadreTable::getUpdateTime,new Date())
                .eq(CadreTable::getCadreNumber,number)
                .set(CadreTable::getPassword,password);
        int update=this.cadreTableMapper.update(wrapper);
        if(update>0){
            return Result.success();
        }
        return Result.fail();
    }
}




